﻿using System;
using System.Collections.Generic;


namespace IQIGame.Onigao.Framework
{
    /// <summary>
    /// 应当在自己的程序集平铺所有的池
    /// <code>
    /// public static class ListPools {
    ///    public static readonly ListPoolImpl《int》 guidPool = new ListPoolImpl《int》();
    /// }
    /// </code>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ListPoolImpl<T>
    {
        private readonly Stack<List<T>> pool = new Stack<List<T>>();
        private int _poolSize;

        public ListPoolImpl(int poolSize = 20) {
            this._poolSize = poolSize;
        }

        public int PoolSize {
            get => _poolSize;
            set {
                if (value < 0) throw new ArgumentException();
                _poolSize = value;
            }
        }

        public List<T> Get() {
            if (pool.Count > 0) {
                return pool.Pop();
            }
            List<T> list = new List<T>();
            return list;
        }

        public void Put(List<T> list) {
            if (list == null) {
                LogGame.LogError("ListPool Put list is null");
                return;
            }

            list.Clear();
            if (pool.Count >= _poolSize)
                return;
            pool.Push(list);
        }

        public void Clear() {
            pool.Clear();
        }
    }
}